package com.ch.forum.service.impl;

import com.ch.forum.bo.PageResult;
import com.ch.forum.bo.ResponseResult;
import com.ch.forum.dao.ArtHistoryMapper;
import com.ch.forum.dao.UserMapper;
import com.ch.forum.model.UserModel;
import com.ch.forum.service.SystemParamService;
import com.ch.forum.service.UserService;
import com.ch.forum.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArtHistoryMapper artHistoryMapper;

    @Autowired
    private SystemParamService systemParamService;

    @Override
    public ResponseResult addUser(UserModel userModel) {

        UserModel isRepeat = userMapper.findByPhone(userModel.getPhone());
        if(isRepeat == null){
            userModel.setIsValid(0);
            userModel.setDate(new Date(System.currentTimeMillis()));
            userModel.setLevel(0);
            userModel.setNickName("用户" + userModel.getPhone());
            userModel.setPoint(0D);
            // 注册既送积分'
            String point = systemParamService.findValueByName("REGIST_VIRTUAL_POINT");
            userModel.setVirtualPoint(Double.valueOf(point));
            int result = userMapper.addUser(userModel);
            if(result > 0){
                return new ResponseResult(true, "注册成功");
            }else {
                return new ResponseResult(false, "注册服务出现问题，请重试");
            }
        }else {
            return new ResponseResult(false, "手机已注册");
        }
    }

    @Override
    public ResponseResult login(String phone, String pwd) {
        UserModel userModel = userMapper.findByPhone(phone);
        if(userModel == null){
            return new ResponseResult(false, "找不到账号");
        }
        if(StringUtils.isEmpty(pwd)){
            return new ResponseResult(false, "密码不能为空");
        }
        if(pwd.equals(userModel.getPwd())){
            return new ResponseResult(true,"登录成功", userModel);
        }else {
            return new ResponseResult(false, "密码错误");
        }
    }

    @Override
    public ResponseResult checkListPage(int pageNo, int pageSize) {
        int pageNum = (pageNo - 1)*pageSize;
        List<UserModel> records = userMapper.checkListPage(pageNum, pageSize);
        int totalRecords = userMapper.checkCount();
        return new ResponseResult(true, "",
                new PageResult(pageNo, pageSize, totalRecords, records));
    }

    @Override
    public ResponseResult checkUser(String ids) {
        if(StringUtils.isEmpty(ids)){
            return new ResponseResult(false, "参数不能为空");
        }else {
            String[] idss = ids.split(",");
            for (String temp : idss){
                userMapper.checkUser(temp);
            }
            return new ResponseResult(true, "已审核");
        }
    }

    @Override
    public ResponseResult vipUser(String ids) {
        if(StringUtils.isEmpty(ids)){
            return new ResponseResult(false, "参数不能为空");
        }else {
            int result = userMapper.vipUser(ids);
            if(result > 0){
                return new ResponseResult(true, "已审核");
            }else {
                return new ResponseResult(false, "审核出现异常，请重试");
            }
        }
    }

    @Override
    public ResponseResult forget(String phone) {
        UserModel userModel = userMapper.findByPhone(phone);
        return new ResponseResult(userModel == null ? false : true ,"密码",userModel == null ? null : (Object) userModel.getPwd());
    }

    @Override
    public ResponseResult updatePwd(String phone, String old, String latest) {
        UserModel userModel = userMapper.findByPhone(phone);
        if(!old.equals(userModel.getPwd())){
            return new ResponseResult(false,"旧密码不正确");
        }
        userMapper.updatePwd(latest, phone);
        return new ResponseResult(true, "修改成功");
    }

    @Override
    public ResponseResult updateUserInfo(Long userId, String nickName, String imgUrl, String phone) {
        UserModel userModel = userMapper.findByPhone(phone);
        if(userModel != null){
            return new ResponseResult(false, "手机号已被占用");
        }
        userMapper.updateUserInfo(phone, nickName, imgUrl, userId);

        return new ResponseResult(true, "返回数据",userMapper.findByPhone(phone));
    }

    @Override
    public ResponseResult checkValid(Long userId) {

        int valid = userMapper.getValidByUserId(userId);
        if(valid == 0){
            return new ResponseResult(false, "未审核账号无法发布帖子");
        }
        if(valid == 1){
            Long num = artHistoryMapper.countNum(userId);
            if(num.intValue() >= 3){
                return new ResponseResult(false, "非会员只能发布3个帖子");
            }
        }
        return new ResponseResult(true, "可以正常发帖");
    }

    @Override
    public ResponseResult checkDealPwd(Long id, String dealPwd) {
        UserModel model = userMapper.findById(id);
        if(model == null){
            return new ResponseResult(false, "找不到此账号");
        }
        if(StringUtils.isEmpty(model.getDealPwd())){
            return new ResponseResult(false, "请先设置交易密码");
        }
        if(StringUtils.isEmpty(dealPwd)){
            return new ResponseResult(false, "参数不能为空");
        }
        if(!dealPwd.equals(model.getDealPwd())){
            return new ResponseResult(false, "交易密码不正确");
        }else {
            return new ResponseResult(true, "核对成功");
        }
    }

    @Override
    public ResponseResult findUserLists() {
        return new ResponseResult(true, "数据", userMapper.findUserLists());
    }

    @Override
    public ResponseResult findUserById(Long id) {
        if(id == null){
            return new ResponseResult(false, "参数不能为空");
        }
        UserModel model = userMapper.findById(id);
        if(model == null){
            return new ResponseResult(false, "查找不到用户，请确认参数");
        }
        return new ResponseResult(true,"数据", model);
    }
}
